import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface ErrorsState {
  openDialog: boolean;
  openErrorDialogBox: boolean; // Alias for openDialog for compatibility
  errorMessage: string;
  errorDetails: string;
}

const initialState: ErrorsState = {
  openDialog: false,
  openErrorDialogBox: false,
  errorMessage: '',
  errorDetails: '',
};

const errorsSlice = createSlice({
  name: 'errors',
  initialState,
  reducers: {
    openBackendErrorDialog: (state, action: PayloadAction<{ errorMessage: string; errorDetails: string }>) => {
      state.openDialog = true;
      state.openErrorDialogBox = true;
      state.errorMessage = action.payload.errorMessage;
      state.errorDetails = action.payload.errorDetails;
    },
    closeBackendErrorDialog: (state) => {
      state.openDialog = false;
      state.openErrorDialogBox = false;
      state.errorMessage = '';
      state.errorDetails = '';
    },
  },
});

export const { openBackendErrorDialog, closeBackendErrorDialog } = errorsSlice.actions;

export default errorsSlice.reducer;

